﻿'Title:     Selection Structures Lab
'Author:    Greg Cronk
'Date:      03/11/2014
'Course:    CSC 162
'Section:   401
'Description:   This program allows the user to enter grades for multiple students into input boxs for each grade then outputs an average and asks if if there are more students.
'               A Sentinal Loop check if the user says "Y" and restarts the loop.  A Class Average for the term is outputed as well as indvidual student averages.
'
'Initial Algorithm:
'   1.  Find out if the user is ready to enter grades
'   2.  Get the Student Name
'   3.  Get the Quiz Average, Homework Average, and Final Exam Grade
'   4.  Calculate the Term Grade based on the Individual Student Formula
'   6.  Output the Individual Average and Update the Class Average
'   7.  Ask if there are more students grades to be entered, if Yes repeat this process and append the Individual Student average and Update the Class Average.
'
'Data Requirements:
'   Input:
'       Student Name, Quiz Average, Homework Average, and Final Grade.  Additional Students to be Entered
'   Output:
'       Term Grade and Class Grade.
'   
'
'Formulas:
' Indvidual Students :  (Quiz Average * .3) + (Homework Average * .2) + (Final Exam * .5)
' Raw Class Average  :  (Raw Class Average + Student Term Grade)
' Class Average      :  (Raw Average / Number of students entered)
'
'Refined Algorithm In Subs as used.

Public Class frmGradeCal

    'Global Declarations
    Dim ClassAvg As Single 'Variable to hold the Class Average. Used in multiple subroutines and must be declared Globally
    Dim RawClass As Single 'Variable holds the total of the various students final grades. used in multiple subroutines so declared globally

    Private Sub frmGradeCal_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Hide the Prompt to enter grades on Load
        lblPrompt.Visible = False
    End Sub

    Private Sub txtGetStudents_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtGetStudents.KeyPress
        'Refind Algorithm
        'On Key Press Check to see if the Entry is equal to our Sentinal Flag for our loop
        '  Make the Prompt label visible
        '  Disable the Entry Box on the Main Form
        '  Set the Sentinal to "Y" our Flag for the First Run
        '    While our Sentinal Flag = "Y"
        '      Ask for the Student Name 
        '       If the student Name is Not Numeric Then begin Collecting Data
        '          Prompt for the Quiz Average
        '          Prompt fo the Homework Average
        '          Prompt for the Final Exam Grade
        '          Calculate our Student Final Term Average
        '          Increase our Loop Counter of how many students grades are entered by 1
        '          Output the rounded Term Grade for the Student in lblStudGrade
        '          Add the Student Term Grade to our Class Raw Score.
        '          Calculate the Class Average by dividing the Raw Score by the Loop Counter.
        '             If we have only 1 student's grades so far. Display the Class Average stating it is for single student.
        '             Else if we have more than one student.  Display the Class Average stating it is for multiple student(s).
        '             End If Plurality Check
        '       Else if the student's name entered is Numeric
        '         Display an Error Message.
        '       End If our Numeric Check
        '      Ask if there are more students to be entered.
        '       If there are repeat our While Loop.
        '       End While if there are no more students grades to be entered.
        '  Hide our Prompt Label 
        '  Re-Enable our Input Box


        'Declare Variables.
        Dim StudFinalAvg As Single 'Variable to hold a Students Final Exam Grade as a Single.
        Dim StudName As String 'Variable to hold the name of the Student as a String
        Dim QuizAvg As Single 'Variable to Hold the Quiz Average of a student as a Single
        Dim HomeworkAvg As Single 'Variable to hold the Homework Average as a Single
        Dim StudentCount As Single 'Variable to hold a running count of how many students grades were entered to calculate our Class Average.
        Dim StudFinal As Single 'A Students Final grade.
        Dim strContinue As String  'Variable to be entered by User to Control our Loop.
        Dim Message As String
        Const strFlag As String = "Y" 'Our sentinal Flag

        If e.KeyChar = strFlag Then 'Check for "Y" to be entered in the Entry Field
            lblPrompt.Visible = True 'make the Prompt Visible
            txtGetStudents.Enabled = False 'Disable the Entry Field
            strContinue = "Y" 'Set the value of our Sentinal the first time to start the loop

            While strContinue.ToUpper = strFlag 'If the Sentinal's Value = the flag start the loop
                StudName = InputBox("Please Enter the Student Name", "Enter Student Name") 'Get the Students Name

                If Not IsNumeric(StudName) Then 'Check that we aren't grading a Number, Numbers aren't People!
                    QuizAvg = InputBox("Please Enter the Quiz Average for " & StudName, StudName & "'s Quiz Average") 'Get the Quiz Average
                    HomeworkAvg = InputBox("Please Enter the Homework Average for " & StudName, StudName & "'s Homework Average") 'Get the Homework Average
                    StudFinalAvg = InputBox("Please Enter the Final Exam Grade for " & StudName, StudName & "'s Final Exame Grade") 'Get the Final Grade Average
                    StudFinal = (QuizAvg * 0.3) + (HomeworkAvg * 0.2) + (StudFinalAvg * 0.5) 'Calculate the Final Student Average
                    StudentCount += 1 'Increase the Loop count for how many students we've entered grades for.
                    lblStudGrade.Text = lblStudGrade.Text & StudName & "'s Final Average is: " & Math.Round(StudFinal - 0.005, 2) & ControlChars.NewLine 'Display the Students Term Grade
                    RawClass = RawClass + StudFinal 'Add the Students Term Grade to a running total.
                    ClassAvg = (RawClass / StudentCount) 'Calculate the Class Average.

                    If StudentCount = 1 Then 'One or less students are not a Plurality!
                        lblClassGrade.Text = "The Class Average for " & StudentCount & ControlChars.NewLine & "Student is " & ClassAvg 'Display the class average based on a single student
                    Else 'More than one student is a Plurality!
                        lblClassGrade.Text = "The Class Average for " & StudentCount & ControlChars.NewLine & "Students is " & ClassAvg 'Display the Class average based on multiple students.
                    End If

                Else
                    Message = MsgBox("Indviduals are not named Numerically, Please enter the Student Name", MsgBoxStyle.Exclamation, "Error") 'Explain that people are not numbers!

                End If
                strContinue = InputBox("Are You ready for the Next Student? (Y/N)", "Continue?").ToUpper 'Check to see if we continue our loop

            End While
            lblPrompt.Visible = False 'On breaking our Loop Hide the Prompt Label

        End If

    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click

        'Clear various variables and hide and re-enable specific controls as necessary
        txtGetStudents.Enabled = True 'Re-enable for a fresh start
        lblPrompt.Visible = False 'Hide the prompt until called again
        ClassAvg = 0 'reset the running total
        RawClass = 0 'reset the running total
        lblClassGrade.Text = String.Empty 'Empty the label
        lblStudGrade.Text = String.Empty 'Empty the label
        txtGetStudents.Focus() 'set focus back on the Entry Box
        txtGetStudents.Text = String.Empty 'Clear any text in the entry box

    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'Close the Program and release the Memory
        Me.Close()
        End
    End Sub
End Class
